package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.admin.api.entity.SysDept;
import com.engine4cloud.tdf.admin.api.vo.AreaResultVO;
import com.engine4cloud.tdf.common.core.constant.enums.NormalAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.supervision.dto.create.SpMigrateCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpMigrateResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpMigrateUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.SpMigrateEntity;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface SpMigrateConvert {
    SpMigrateConvert INSTANCE = Mappers.getMapper(SpMigrateConvert.class);

    SpMigrateResultDTO convertTo(SpMigrateEntity s);

    SpMigrateEntity createTo(SpMigrateCreateDTO s);

    SpMigrateEntity updateTo(SpMigrateUpdateDTO s);

    default SpMigrateEntity create(SpMigrateCreateDTO s, AreaResultVO sourceCounty, AreaResultVO sourceVillage, AreaResultVO targetVillage, SysDept targetDept, Long tenantId, NormalAuditActionStatusEnum action) {
        SpMigrateEntity entity = createTo(s);
        entity.setLcbh(CodeUtils.entrustCode());
        entity.setPreAction(action);
        entity.setTenantId(tenantId);
        entity.setYjzdqx(sourceCounty.getCode());
        entity.setYjzdqxmc(sourceCounty.getName());
        entity.setYjzdxz(sourceVillage.getAreaId());
        entity.setYjzdxzmc(sourceVillage.getName());
        entity.setQrdqx(sourceCounty.getCode());
        entity.setQrdqxmc(sourceCounty.getName());
        entity.setQrdxz(targetVillage.getAreaId());
        entity.setQrdxzmc(targetVillage.getName());
        entity.setQrdswtdsfs(targetDept.getDeptId());
        entity.setQrdswtdsfsmc(targetDept.getName());
        return entity;
    }

    default SpMigrateEntity update(SpMigrateUpdateDTO s, AreaResultVO sourceCounty, AreaResultVO sourceVillage, AreaResultVO targetVillage, SysDept targetDept) {
        SpMigrateEntity entity = updateTo(s);
        entity.setYjzdqx(sourceCounty.getCode());
        entity.setYjzdqxmc(sourceCounty.getName());
        entity.setYjzdxz(sourceVillage.getAreaId());
        entity.setYjzdxzmc(sourceVillage.getName());
        entity.setQrdqx(sourceCounty.getCode());
        entity.setQrdqxmc(sourceCounty.getName());
        entity.setQrdxz(targetVillage.getAreaId());
        entity.setQrdxzmc(targetVillage.getName());
        entity.setQrdswtdsfs(targetDept.getDeptId());
        entity.setQrdswtdsfsmc(targetDept.getName());
        return entity;
    }

    default SpMigrateResultDTO convert(AreaResultVO city, SpMigrateEntity s) {
        if (Objects.isNull(s)) {
            return null;
        }

        SpMigrateResultDTO result = convertTo(s);
        if (Objects.nonNull(city)) {
            List<String> sourceAreaList = new ArrayList<>();
            sourceAreaList.add(city.getPcode());
            sourceAreaList.add(city.getCode());
            sourceAreaList.add(s.getYjzdqx());
            sourceAreaList.add(String.valueOf(s.getYjzdxz()));
            result.setSourceAreaList(sourceAreaList);

            List<String> targetAreaList = new ArrayList<>();
            targetAreaList.add(city.getPcode());
            targetAreaList.add(city.getCode());
            targetAreaList.add(s.getQrdqx());
            targetAreaList.add(String.valueOf(s.getQrdxz()));
            result.setTargetAreaList(targetAreaList);
        }
        return result;
    }
}
